package com.lwj.liwanjia.view.activity.myindent;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.MyIndentBean;
import com.lwj.liwanjia.bean.PingjiaBean;
import com.lwj.liwanjia.impl.PingjiaView;
import com.lwj.liwanjia.presenter.ZhuijiaPingjiaPresenter;
import com.lwj.liwanjia.utils.SpfUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ZhuijiaPjActivity extends BaseActivity implements PingjiaView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_shangpin)
    ImageView ivShangpin;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_jianjie)
    TextView tvJianjie;
    @BindView(R.id.tv_jiage)
    TextView tvJiage;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.ed_pingjia)
    EditText edPingjia;
    @BindView(R.id.tv_zishu)
    TextView tvZishu;

    private String token;
    private ZhuijiaPingjiaPresenter presenter;
    private Map<String, Object> map = new HashMap<>();
    private MyIndentBean.DataBean.GoodsBean.CommodityBean commodityBean;
    private String tradeId;

    @Override
    protected void initView() {
        MyIndentBean.DataBean.GoodsBean goods = (MyIndentBean.DataBean.GoodsBean) getIntent().getSerializableExtra("infor");
        tradeId = goods.getTradeId();
        commodityBean = goods.getCommodity().get(0);
        Glide.with(this).load(commodityBean.getCommodityimage()).error(R.mipmap.tupian).into(ivShangpin);
        tvName.setText(commodityBean.getCommodityname());
        tvJianjie.setText(commodityBean.getCommodityinfo());
        tvJiage.setText("￥" + commodityBean.getCommodityprice());
        tvNum.setText("*" + commodityBean.getCommodityNum());
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("发表追评");
        presenter = new ZhuijiaPingjiaPresenter(this);
        token = SpfUtils.getSpfUtils(this).getToken();
        listener();
    }

    private void listener() {
        edPingjia.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvZishu.setText(s.length() + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_zhuijia_pj;
    }


    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void pingjiaIndent(PingjiaBean pingjiaBean) {
        if (pingjiaBean.getState() == 0) {
            Toast.makeText(this, "评价失败", Toast.LENGTH_SHORT).show();
        } else if (pingjiaBean.getState() == 1) {
            if (pingjiaBean.isData()) {
                Toast.makeText(this, "评价成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }


    @OnClick({R.id.tv_left, R.id.tv_zhuiping})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_zhuiping:
                map.put("token", token);
                map.put("orderId", tradeId);
                if (TextUtils.isEmpty(edPingjia.getText().toString())) {
                    Toast.makeText(this, "评价内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                map.put("addCommentContent", edPingjia.getText().toString());
                presenter.zhuijiaPingjia(map);
                break;
        }
    }
}
